package com.zhkc.iess.online.test01.task;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

public class ExecutionContextContainer {
    private ConcurrentMap<String, ExecutionContextImpl> contextMap = new ConcurrentHashMap();

    public void add(ExecutionContextImpl exeCtx) {
        ExecutionContextImpl old = this.contextMap.putIfAbsent(exeCtx.getQN(), exeCtx);

        //putIfAbsent 如果传入key对应的value已经存在，就返回存在的value，不进行替换。如果不存在，就添加key和value，返回null
        if (old != null) {// 33
            throw new IllegalStateException("Duplicated invocation id:" + exeCtx.getQN());
        }
    }

    public void remove(String qn) {
        this.contextMap.remove(qn);
    }

    public ExecutionContextImpl get(String qn) {
        return this.contextMap.get(qn);
    }
}